//
//  LSWelcomeViewController.swift
//  新浪微博---01次
//
//  Created by 李宗帅 on 16/5/6.
//  Copyright © 2016年 LS. All rights reserved.
//

import UIKit

import SnapKit
class LSWelcomeViewController: UIViewController {
    
    //让背景填充这个视图
    override func loadView() {
        view = bgImageView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupUI()
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    private func setupUI(){
        view.addSubview(photoImageView)
        view.addSubview(nameLabel)
        
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(view.snp_top).offset(UIScreen.mainScreen().bounds.height - 250)
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(photoImageView.snp_bottom).offset(10)
        }
    }
    
    private lazy var bgImageView: UIImageView = {
       let img = UIImageView()
        
        img.image = UIImage(named: "ad_background")
        
        return img
    }()
    
    private lazy var photoImageView: UIImageView = {
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
        
        //设置圆角
        img.layer.cornerRadius = 170/2/2
        
        img.layer.borderColor = UIColor.darkGrayColor().CGColor
        
        img.layer.borderWidth = 2
        
        img.layer.masksToBounds = true
        
        return img
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        
        label.text = "欢迎归来"
        
        label.textColor = UIColor.lightGrayColor()
        
        label.font = UIFont.systemFontOfSize(15)
        
        label.numberOfLines = 0
        
        return label
    }()
    
    //添加动画
    private func startAnimation(){
        
         //更新约束用update
        self.photoImageView.snp_updateConstraints(closure: { (make) -> Void in
            make.top.equalTo(self.view.snp_top).offset(100)
        })
        //弹簧的动画
        //usingSpringWithDamping --- 弹簧的抖动系数 0.0~1.0
        //initialSpringVelocity -- 弹簧初始化的速度
        //枚举 oc里默认值写0  swfit里写[]
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            //动画执行
           
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                //动画完成.切换视图控制器
            NSNotificationCenter.defaultCenter().postNotificationName(kNotificationChangViewController, object: self)
        }
    }
}
